//页面初始化
$(function () {
    //加载图片
    $(".filejs").click(function(){
        $(".file").click();
    })
    //加载数据
    queryBuildingInfo();
})

//查询楼苑信息列表
function  queryBuildingInfo() {
    $.ajax({
        url:getRootPath_web()+'baseData/building/pagedQueryBuildingInfo',
        type:'post',
        dataType:'json',
        success:function (result) {
            $(".provide-dl").empty();
            if(result.totalRecord>0){
                $.each(result.buildingInfo,function (key,b) {
                    var str="";
                    str+="<dl id='"+b.buildingNo+"' class=\"box-shadows2 mb20px\">";
                    if(isCURD){
                        str+="<dt data-id='"+b.buildingNo+"' onclick='toFloor(this)'>";
                    }else {
                        str+="<dt data-id='"+b.buildingNo+"'>";
                    }

                    if(b.buildingImg!=null&&b.buildingImg!=''){
                        str+="<img src='"+b.buildingImg+"' >";
                    }else {
                        str+="<img src=  '"+getRootPath_web()+"static/img/defaultBuilding.png' >";
                    }
                    str+="<dd>";
                    str+="<div class=\"overflow mt10px mb15px\">";
                    str+="<p class=\"left f16px\">"+b.buildingName+"</p>";
                    if(isCURD) {
                        str += "<p class=\"right table-list-btn\">";
                        str += "<span data-id='" + b.buildingNo + "' class='cursor' onclick='updateBuilding(this)'>编辑</span>";
                        str += "|<span data-id='" + b.buildingNo + "' onclick='deleteBuilding(this)' class='red cursor'>删除</span>";
                        str += "</p>";
                    }else {
                        str += "<p class=\"right table-list-btn\">";
                        str += "<span data-id='" + b.buildingNo + "' style='color: gray' >编辑</span>";
                        str += "|<span data-id='" + b.buildingNo + "' style='color: gray'>删除</span>";
                        str += "</p>";
                    }
                    str+="</div>";
                    if(isCURD){
                        str+="<ul class='cursor' data-id='"+b.buildingNo+"' onclick='toFloor(this)'>";
                    }else {
                        str+="<ul data-id='"+b.buildingNo+"'>";
                    }
                    if(b.totalFloor>0){
                        str+="<li>楼层：<span class=\"green\">"+b.totalFloor+"</span>层</li>";
                        if(b.totalBed>0){
                            str+="<li>总床位：<span class=\"green\">"+b.totalBed+"</span>个</li>";
                            str+="<li>空床位：<span class=\"green\">"+b.emptyBed+"</span>个</li>";
                        }else {
                            str+="<li>总床位：<span class=\"green\">?</span>个</li>";
                            str+="<li>空床位：<span class=\"green\">?</span>个</li>";
                        }
                    }else {
                        str+="<li>楼层：<span class=\"green\">?</span>层</li>";
                        str+="<li>总床位：<span class=\"green\">?</span>个</li>";
                        str+="<li>空床位：<span class=\"green\">?</span>个</li>";
                        if(isCURD){
                            str+="<li data-id='"+b.buildingNo+"' onclick='toFloor(this)' class=\"textRight red cursor\">*此为新添加楼苑，请点击编辑进行楼栋设置</li>";
                        }else {
                            str+="<li data-id='"+b.buildingNo+"' class=\"textRight red cursor\">*此为新添加楼苑，请点击编辑进行楼栋设置</li>";
                        }

                    }
                    str+="</ul>";
                    str+="</dd>";
                    str+="</dt>";
                    $(".provide-dl").append(str);
                })
            }else {
                var str="<div class='empty-div'>暂无楼苑信息</div>";
                $(".provide-dl").append(str);
            }
        }
    })
}

//楼苑添加
function addBuilding() {
    var str="<div class=\"pop-up\">";
    str+="<div>";
    str+="<div class=\"pop-title relative overflow mb15px\">";
    str+="<span class=\"ml15px\">新建楼苑</span>";
    str+="<p class=\"right closeBtn\" style='cursor: pointer'><img src='"+getRootPath_web()+"static/img/close_icon.png' class=\"block pop-close\"><p>";
    str+="</div>";
    str+="<form id='buildingForm'>";
    str+="<div class=\"pop-form mt30px mb30px\">";
    str+="<div class=\"layui-form-item\">";
    str+="<label class=\"layui-form-label\"><span class=\"red\">*</span>楼苑名称</label>";
    str+="<div class=\"layui-input-block\">";
    str+="<input type=\"text\" name='buildingName' maxlength='12' placeholder=\"楼苑名称\" class=\"layui-input buildingName\">";
    str+="</div>";
    str+="</div>";
    str+="<div class=\"layui-form-item\">";
    str+="<label class=\"layui-form-label\"><span class=\"red\">*</span>面向人群</label>";
    str+="<div class=\"layui-input-block\">";
    str+="<input type=\"text\" name='buildingUse' maxlength='30' x placeholder=\"面向人群\" class=\"layui-input buildingUse\">";
    str+="</div>";
    str+="</div>";

    str+="<div class=\"layui-form-item\">";
    str+="<label class=\"layui-form-label\">上传照片</label>";
    str+="<div class=\"layui-input-block\">";
    str+="<ul>";
    str+="<li class=\"filejs\">";
    str+="<img src='"+getRootPath_web()+"static/img/file.png' style=\"width: 132px; height: 80px;\" class=\"border-radius4 fileImg\">";
    str+="<input type=\"file\" name='file' accept='imag/*' id='fileInput' class=\"hidden file\"/>";
    str+="</li>";
    str+="<li class=\"lightGray mt5px\">支持jpg/jpeg/png格式，推荐尺寸132*80px</li>";
    str+="</ul>";
    str+="</div>";
    str+="</div>";
    str+="</div>";
    str+="<div class=\"btn-con textCenter\">";
    str+="<span class=\"btn-two btn-submit cursor\">提&nbsp;交</span>";
    str+="</div>";
    str+="</div>";
    str+="</form>";
    str+="</div>";

    layui.use(['layer'], function() {
        var layer = layui.layer;
        layer.open({
            type: 1,
            title: false, //不显示标题栏
            closeBtn: false,
            area: ['500px', ''],
            shade: 0.6,
            id: 'payCons', //设定一个id，防止重复弹出
            /*,btn: ['确认支付']*/
            btnAlign: 'c',
            moveType: 1, //拖拽模式，0或者1
            shadeClose: false, //设置允许点遮罩关闭
            content: str,
            success: function() {
                //加载图片
                $(".fileImg").click(function(){
                    $(".file").click();
                })
                //图片预览
                $("#fileInput").change(function() {
                    var file = this.files[0];
                    var r = new FileReader();
                    r.readAsDataURL(file);
                    $(r).load(function() {
                        $(".fileImg").attr('src',this.result);
                    });
                });
                //关闭弹层
                $(".closeBtn").click(function() {
                    layer.closeAll();
                });
                $(".btn-submit").click(function () {
                    saveBuildingInfo();
                })
            },
        });
    });
}
//保存楼宇信息
function saveBuildingInfo() {
    var buildingForm = new FormData(document.getElementById("buildingForm"));
    layui.use('layer', function(){
        var layer = layui.layer;
        //楼苑名称
        var buildingName = $(".buildingName").val();
        if(buildingName==''){
            layer.msg('楼苑名称不能为空！');
            return false;
        }
        //面向人群
        var buildingUse = $(".buildingUse").val();
        if(buildingUse==''){
            layer.msg('面向人群不能为空！');
            return false;
        }
        //获取图片
        var imageFile = document.getElementById("fileInput").value;
        if (imageFile != '') {
            //验证图片格式
            if (!/\.(jpg|jpeg|png|JPEG|JPG|PNG)$/.test(imageFile)) {
                layer.msg("图片类型必须是jpeg,jpg,png中的一种");
                return false;
            }
        }
        $.ajax({
            url:getRootPath_web()
                +'baseData/building/saveBuildingInfo',
            type:'post',
            dataType:'json',
            data:buildingForm,
            processData: false,
            contentType: false,
            beforeSend:function () {
                loading = layer.load(1, {
                    shade: [0.9,'#fff']
                    ,shadeClose:false
                });
            },
            success:function (result) {
                if(result.code=='0000'){
                    layer.confirm('添加成功', {
                        btn: ['确定'] //按钮
                        ,shadeClose: false
                        ,closeBtn:false
                    }, function(){
                        layer.closeAll();
                        window.location.href=getRootPath_web()
                            +"baseData/building/buildingList";
                    })
                }else {
                    layer.confirm(result.codeDesc, {
                        btn: ['确定'] //按钮
                        ,shadeClose: false
                        ,closeBtn:false
                    }, function(){
                        layer.close(layer.index);
                        layer.close(loading);
                    })
                }
            }
        })
    });
}

//修改楼苑信息
function updateBuilding(object){
    var buildingNo = $(object).attr("data-id");
    $.ajax({
        url:getRootPath_web()+'baseData/building/queryBuildingInfoDetail',
        type:'post',
        dataType:'json',
        data:{"buildingNo":buildingNo},
        success(result){
            if(result.code!='0000'){
                layer.msg(result.codeDesc);
                return false;
            }
            var str="<div class=\"pop-up\">";
            str+="<div>";
            str+="<div class=\"pop-title relative overflow mb15px\">";
            str+="<span class=\"ml15px\">编辑楼苑</span>";
            str+="<p class=\"right closeBtn\" style='cursor: pointer'><img src='"+getRootPath_web()+"static/img/close_icon.png' class=\"block pop-close\"><p>";
            str+="</div>";
            str+="<form id='buildingForm'>";
            str+="<input name='buildingNo' type='hidden' value='"+result.buildingNo+"' />";
            str+="<div class=\"pop-form mt30px mb30px\">";
            str+="<div class=\"layui-form-item\">";
            str+="<label class=\"layui-form-label\"><span class=\"red\">*</span>楼苑名称</label>";
            str+="<div class=\"layui-input-block\">";
            str+="<input type=\"text\" name='buildingName' value='"+result.name+"' maxlength='12' placeholder=\"楼苑名称\" class=\"layui-input buildingName\">";
            str+="</div>";
            str+="</div>";
            str+="<div class=\"layui-form-item\">";
            str+="<label class=\"layui-form-label\"><span class=\"red\">*</span>面向人群</label>";
            str+="<div class=\"layui-input-block\">";
            str+="<input type=\"text\" name='buildingUse' maxlength='30' value='"+result.use+"' placeholder=\"面向人群\" class=\"layui-input buildingUse\">";
            str+="</div>";
            str+="</div>";

            str+="<div class=\"layui-form-item\">";
            str+="<label class=\"layui-form-label\">上传照片</label>";
            str+="<div class=\"layui-input-block\">";
            str+="<ul>";
            str+="<li class=\"filejs\">";
            if(result.img!=null&&result.img!=''){
                str+="<img src='"+result.img+"' style=\"width: 132px; height: 80px;\" class=\"border-radius4 fileImg\">";
            }else {
                str+="<img src='"+getRootPath_web()+"static/img/file.png' style=\"width: 132px; height: 80px;\" class=\"border-radius4 fileImg\">";
            }
            str+="<input type=\"file\" name='file' accept='imag/*' id='fileInput' class=\"hidden file\"/>";
            str+="</li>";
            str+="<li class=\"lightGray mt5px\">支持jpg/jpeg/png格式，推荐尺寸132*80px</li>";
            str+="</ul>";
            str+="</div>";
            str+="</div>";
            str+="</div>";
            str+="<div class=\"btn-con textCenter\">";
            str+="<span class=\"btn-two btn-submit cursor\">修&nbsp;改</span>";
            str+="</div>";
            str+="</div>";
            str+="</form>";
            str+="</div>";
            var loading;
            layui.use(['layer'], function() {
                var layer = layui.layer;
                layer.open({
                    type: 1,
                    title: false, //不显示标题栏
                    closeBtn: false,
                    area: ['500px', ''],
                    shade: 0.6,
                    id: 'payCons', //设定一个id，防止重复弹出
                    /*,btn: ['确认支付']*/
                    btnAlign: 'c',
                    moveType: 1, //拖拽模式，0或者1
                    shadeClose: false, //设置允许点遮罩关闭
                    content: str,
                    success: function() {
                        //加载图片
                        $(".fileImg").click(function(){
                            $(".file").click();
                        })
                        //图片预览
                        $("#fileInput").change(function() {
                            var file = this.files[0];
                            var r = new FileReader();
                            r.readAsDataURL(file);
                            $(r).load(function() {
                                $(".fileImg").attr('src',this.result);
                            });
                        });
                        //关闭弹层
                        $(".closeBtn").click(function() {
                            layer.closeAll();
                        });
                        $(".btn-submit").click(function () {
                            updateBuildingInfo();
                        })
                    },
                });
            });
        }
    })

}
//修改楼苑信息
function updateBuildingInfo() {
    var buildingForm = new FormData(document.getElementById("buildingForm"));
    layui.use('layer', function(){
        var layer = layui.layer;
        //楼苑名称
        var buildingName = $(".buildingName").val();
        if(buildingName==''){
            layer.msg('楼苑名称不能为空！');
            return false;
        }
        //面向人群
        var buildingUse = $(".buildingUse").val();
        if(buildingUse==''){
            layer.msg('面向人群不能为空！');
            return false;
        }
        //获取图片
        var imageFile = document.getElementById("fileInput").value;
        if (imageFile != '') {
            //验证图片格式
            if (!/\.(jpg|jpeg|png|JPEG|JPG|PNG)$/.test(imageFile)) {
                layer.msg("图片类型必须是jpeg,jpg,png中的一种");
                return false;
            }
        }
        var loading;
        $.ajax({
            url:getRootPath_web()
                +'baseData/building/updateBuildingInfo',
            type:'post',
            dataType:'json',
            data:buildingForm,
            processData: false,
            contentType: false,
            beforeSend:function () {
                loading = layer.load(1, {
                    shade: [0.9,'#fff']
                    ,shadeClose:false
                });
            },
            success:function (result) {
                if(result.code=='0000'){
                    layer.confirm('修改成功', {
                        btn: ['确定'] //按钮
                        ,shadeClose: false
                        ,closeBtn:false
                    }, function(){
                        layer.closeAll();
                        window.location.href=getRootPath_web()
                            +"baseData/building/buildingList";
                    })
                }else {
                    layer.confirm(result.codeDesc, {
                        btn: ['确定'] //按钮
                        ,shadeClose: false
                        ,closeBtn:false
                    }, function(){
                        layer.close(layer.index);
                        layer.close(loading);
                    })
                }
            }
        })
    });
}
//删除楼苑信息
function deleteBuilding(obj) {
    var buildingNo = $(obj).attr("data-id");
    layui.use('layer', function(){
        var layer = layui.layer;
        layer.confirm('确定删除该楼苑吗？', {
            btn: ['确定','取消'] //按钮
        }, function(){
            $.ajax({
                url:getRootPath_web()
                    +'baseData/building/deleteBuildingInfo',
                type:'post',
                dataType:'json',
                data:{"buildingNo":buildingNo},
                success:function (result) {
                    if(result.code=='0000'){
                        layer.confirm('删除成功', {
                            btn: ['确定'] //按钮
                            ,shadeClose: false
                        }, function(){
                            $("#"+buildingNo).remove();
                            layer.closeAll();
                        })
                    }else {
                        layer.confirm(result.codeDesc, {
                            btn: ['确定'] //按钮
                            ,shadeClose: false
                        }, function(){
                            layer.close(layer.index);
                        })
                    }
                }
            })
        }, function(){
            layer.close(layer.index);
        });
    });
}
//跳转到楼层列表
function  toFloor(object) {
    var buildingNo = $(object).attr("data-id");
    window.location.href=getRootPath_web()
        +"baseData/floor/floorList?buildingNo="+buildingNo;
}